<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">已爬取的微博管理</div>
                        <div class="layui-card-body">
                            <div class="layui-row layui-col-space15">
                                <div class="layui-col-md12">
                                    <table class="layui-hide" id="weibo_manage"
                                           lay-filter="weibo_manage"></table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

    /**
     * 获取
     * @param id
     */

    jQuery.ajax({
        url: "/weiboDetail/all.do",
        type: "get",
        dataType: "json",
        contentType: "application/json;charset=utf-8"
    }).done(function (data) {
        loadTable(data);
        console.log(data);

    }).fail(function (data) {
        layer.alert("请求失败", {icon: 7});
        layer.close(layerIndex);
    });

    function loadTable(data) {
        var mudata = data.data;
        for (var i = 0; i < mudata.length; i++) {
            mudata[i].id = i + 1;
            mudata[i].publishTime = stamp2Time(mudata[i].publishTime / 1000);
        }

        layui.use('table', function () {
            var table = layui.table
                , form = layui.form;
            table.render({
                elem: '#weibo_manage'
                , data: mudata
                , cellMinWidth: 80
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', title: 'ID', unresize: true, sort: true}
                    , {field: 'userId', title: '发布人的id', sort: true}
                    , {field: 'text', title: '内容', sort: true}
                    , {field: 'publishTime', title: '发布时间', sort: true}
                    , {field: 'devices', title: '发布设备', sort: true}
                    , {fixed: 'right', width: 178, align: 'center', toolbar: '#barDemo', title: "操作栏"}
                ]]
                , page: true
            });


            //监听工具条
            table.on('tool(weibo_manage)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    layer.msg('ID：' + data.id + ' 的查看操作');
                } else if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        deleteWeibo(data.weibo_item_id);
                        obj.del();

                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    markid(data.id);
                    layer.alert("成功", {icon: 1});
                }
            });
        });
    }


    /**
     * 删除
     * @param id
     */
    function deleteWeibo(id) {
        jQuery.ajax({
            url: "/weiboDetail/delete.do",
            type: "get",
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        }).done(function (data) {

            layer.alert("成功", {icon: 2});

        }).fail(function (data) {
            layer.alert("请求失败", {icon: 7});
            layer.close(layerIndex);
        });
    }


    function stamp2Time(timestamp) {

        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        D = date.getDate() + ' ';
        h = date.getHours() + ':';
        m = date.getMinutes() + ':';
        s = date.getSeconds();
        return Y + M + D + h + m + s;
    }
</script>
